//
//  CZOrigunalView.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/27.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class CZOrigunalView: UIView {
    
    private var originalViewBottomConstraint: Constraint?
    
    
    var status: CZStatus? {
        didSet {
            //头像
            iconImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            //昵称
            nameLabel.text = status?.user?.name
            //微博内容
            contentLabel.text = status?.text
            //VIP图标
            vipImageView.image = status?.user?.vipImage
            //认证图标
            enterpriseImageView.image = status?.user?.verifiedImage
            //来源
            sourceLabel.attributedText = status?.sourceArr
            //时间
            timeLabel.text = getTimeStr(status?.created_at ?? "")

            originalViewBottomConstraint?.uninstall()
            
            if status?.pic_urls?.count > 0 {
                pictureView.hidden = false
                pictureView.pic_urls = status?.pic_urls
                self.snp_updateConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusMargin).constraint
                }
            } else {
                pictureView.hidden = true
                self.snp_updateConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(statusMargin).constraint
                }
            }

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        setupUI()
        

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    func getWeibiArr(text: String) -> NSMutableAttributedString {
//        let attr = NSMutableAttributedString(string: text)
//        
//        
//        return attr
//    }
    
    // MARK - 处理时间的方法
    private func getTimeStr(creatAt: String)-> String {
        //Thu Jun 30 19:40:32 +0800 2016
        
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
        //微博时间
        let wbDate = dateFormatter.dateFromString(creatAt)!
        //判断是否是今年
        let isThisYear: Bool = getWbIsThisYear(wbDate)
        
        if isThisYear {//是今年
            let calender = NSCalendar.currentCalendar()
            //秒数差值
            let s = NSDate().timeIntervalSinceDate(wbDate)
            
            if calender.isDateInToday(wbDate) {//是今天
                if s < 60 {//小于60秒
                    return "刚刚"
                } else if s >= 60 && s < 60 * 60 {//s>=60 && s < 60 * 60
                    return "\(Int(s / 60))分钟前"
                } else {//大于一个小时
                    return "\(Int(s / 3600))小时前"
                }
            } else if calender.isDateInYesterday(wbDate) {//昨天
                dateFormatter.dateFormat = "昨天 HH:mm"
                return dateFormatter.stringFromDate(wbDate)
            } else {//其他
                dateFormatter.dateFormat = "MM月dd日 HH:mm"
                return dateFormatter.stringFromDate(wbDate)
            }
            //显示格式: 2015年05月05日 10:10
        } else {//不是今年
            dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"
            return dateFormatter.stringFromDate(wbDate)
        }
    }
    // MARK - 判断是否是今年的方法
    private func getWbIsThisYear(wbDate: NSDate)-> Bool {
        
        let df = NSDateFormatter()
        df.dateFormat = "yyyy"
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        let wbDateStr = df.stringFromDate(wbDate)
        let currentDateStr = df.stringFromDate(NSDate())
        
        
        if wbDateStr == currentDateStr {
            
            return true
        } else {
            return false
        }
    }
        
    // MARK - 设置UI
    private func setupUI() {
        //添加子控件
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(enterpriseImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        iconImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 35, height: 35))
            make.top.left.equalTo(self).offset(statusMargin)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp_right).offset(statusMargin)
            make.top.equalTo(iconImageView)
        }
        vipImageView.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp_right).offset(statusMargin)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(iconImageView)
            make.left.equalTo(nameLabel)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(10)
        }
        enterpriseImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImageView.snp_right)
            make.centerY.equalTo(iconImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(statusMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusMargin)
            make.left.equalTo(contentLabel)
        }
        //给自己添加约束
        self.snp_makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusMargin).constraint
        }
        
    }
    // MARK - 懒加载控件
    //头像
    private lazy var iconImageView: UIImageView = UIImageView(imagName: "avatar_default_big")
    //昵称
    private lazy var nameLabel: UILabel = {
        
        let lab: UILabel = UILabel(fontSize: 15, textColor: UIColor.lightGrayColor())
        return lab
    }()
    //VIP图标
    private lazy var vipImageView: UIImageView = UIImageView(imagName: "avatar_vgirl")
    //发布时间
    private lazy var timeLabel: UILabel = {

        let lab = UILabel(fontSize: 10, textColor: ThemeColor)
        lab.text = "发布时间"
        return lab
    }()
    //来源
    private lazy var sourceLabel: UILabel = {
        let lab = UILabel(fontSize: 10, textColor: UIColor.lightGrayColor())
        lab.text = "来源"
        return lab
    }()
    //认证
    private lazy var enterpriseImageView: UIImageView = UIImageView(imagName: "avatar_enterprise_vip")
    //微博内容
    private lazy var contentLabel: UILabel = {
       
        let lab = UILabel(fontSize: 14, textColor: UIColor.lightGrayColor(),maxWidth: SCREENW - 2 * statusMargin)
        return lab
    }()
    //原创微博配图
    private lazy var pictureView: CZPictureView = CZPictureView()
}

